import React, { Component } from 'react';
import {
    Image,
    View,
    Text,
    Dimensions,
    StyleSheet,
    AppRegistry,
    Platform,
    ToastAndroid,
    BackHandler,
    Alert,
    Modal
} from 'react-native';
import { Container, Content, Spinner, List, ListItem, Button, Form, Item, Input, Label, Toast, Body } from 'native-base';
const { width, height } = Dimensions.get('window');
import { connect } from 'react-redux';
import URL from '../../net/url'
import Set_Net from '../../net/setting'
import db_Cache from '../../db/student_table'
export default class modifyPass extends Component {
    constructor(props) {
        super(props);
        this.state = {
            oldpassword: null,
            newpassword: null,
            newpasswordTwo: null,
            wait_state: false
        }
    }
    //修改密码
    async alterPassword() {
        let dbCache = new db_Cache();
        let info = await dbCache.queryStudentInfo();
        if (info !== undefined) {
            let item = info.item(0);
            let old_password = item.password;
            let username = item.num;
            // alert(this.state.oldpassword);
            if (this.state.oldpassword == null || this.state.newpassword == null || this.state.newpasswordTwo == null) {
                this.setState({ wait_state: false });
                Alert.alert(
                    '',
                    '不可为空，请填写完整',
                );
            } else if (old_password != this.state.oldpassword) {
                this.setState({ wait_state: false });
                Alert.alert(
                    '',
                    '您输入的旧密码有误，请重试！',
                );
            } else if (this.state.oldpassword == this.state.newpassword) {
                this.setState({ wait_state: false });
                Alert.alert(
                    '',
                    '不可修改为原有密码！',
                );
            } else if (this.state.newpassword != this.state.newpasswordTwo) {
                this.setState({ wait_state: false });
                Alert.alert(
                    '',
                    '您输入的新密码两次不一致，请确认！',
                );
            } else {
                Alert.alert(
                    '',
                    '确定修改密码？',
                    [
                        { text: '取消', onPress: () => console.log('Cancel Pressed!') },
                        { text: '确定', onPress: () => this.modifyPass() },
                    ]
                );

            }
        } else {
            this.setState({ wait_state: false });
            Alert.alert(
                    '',
                    '不可修改密码',
                );
        }
    }
    //修改密码方法
    async modifyPass() {
        // this.setState({ wait_state: true });
        let dbCache = new db_Cache();
        let info = await dbCache.queryStudentInfo();
        if (info !== undefined) {
            let item = info.item(0);
            let old_password = item.password;
            let username = item.num;
            // alert(this.state.oldpassword);
            try {
                // alert(username+old_password+this.state.newpassword);
                let modifypass_net = new Set_Net();
                var modifypass_result = await modifypass_net.alertPassword(username, old_password, this.state.newpasswordTwo);
                // alert(modifypass_result);
                this.setState({ wait_state: false });
                if (modifypass_result != undefined && modifypass_result.indexOf('successfully!')>=0) {
                    Alert.alert(
                        '',
                        '密码修改成功！',
                        [
                            {
                                text: '确定', onPress: () => {
                                    dbCache.deleteStudentInfo();
                                    const { navigate } = this.props.navigation;
                                    navigate('Login');
                                }
                            },
                        ]
                    );
                } else {
                    Alert.alert(
                        '',
                        '密码修改失败！',
                    );
                }
            } catch (error) {
                console.log(error);
            }
        }
    }

    render() {
        return (
            <Container>
                <Content>
                    <Modal
                        transparent={true}
                        visible={this.state.wait_state}
                        onRequestClose={() => { }}
                    >
                        <Content style={{
                            backgroundColor: 'rgba(0, 0, 0, 0.5)'
                        }}
                            contentContainerStyle={{
                                flex: 1,
                                flexDirection: 'row',
                                alignItems: 'center',
                                justifyContent: 'center'
                            }}>
                            <Spinner style={{ alignSelf: 'center', alignItems: 'center' }} />
                        </Content>
                    </Modal>
                    <Form>
                        <Item style={StyleSheet.flatten(styles.item)} regular>
                            <Input secureTextEntry placeholder='请输入旧密码' onChangeText={(v) => { this.setState({ oldpassword: v }) }} />
                        </Item>
                        <Item style={StyleSheet.flatten(styles.itemm)} regular>
                            <Input secureTextEntry placeholder='请输入新密码' onChangeText={(v) => { this.setState({ newpassword: v }) }} />
                        </Item>
                        <Item style={StyleSheet.flatten(styles.itemm)} regular>
                            <Input secureTextEntry placeholder='请再次输入新密码' onChangeText={(v) => { this.setState({ newpasswordTwo: v }) }} />
                        </Item>
                    </Form>
                    <Button block style={StyleSheet.flatten(styles.button_default)}
                        onPress={() => { this.alterPassword() }}><Text style={StyleSheet.flatten(styles.exittext)} >修改</Text>
                    </Button>
                </Content>

            </Container>
        );
    }
}
const styles = StyleSheet.create({
    exitButton: {
        marginTop: 15,
        width: width,
        backgroundColor: '#ffffff',
        alignSelf: 'center',
    },
    itemm: {
        marginLeft: 25,
        marginRight: 25,
        marginTop: 5,
        backgroundColor: '#ffffff',
    },
    item: {
        marginLeft: 25,
        marginRight: 25,
        marginTop: 30,
        backgroundColor: '#ffffff',
    }
    ,
    exittext: {
        alignItems: 'center',
        alignSelf: 'center',
        color: '#000000',
        textAlignVertical: 'center',
    },
    button_default: {
        marginTop: 15,
        width: width / 2,
        marginLeft: 25,
        marginRight: 25,
        backgroundColor: '#ffffff',
        alignSelf: 'center',
    },
    text_style: {
        //fontFamily: '微软雅黑',
        fontSize: 15,
        color: "#000"

    },
    ListItem2: {
        flexDirection: 'row',
        justifyContent: 'center',
        marginRight: 0,
        marginLeft: 0,
        paddingLeft: 10,
        alignItems: 'center',
        backgroundColor: 'white',
        borderBottomWidth: 0.3,
        borderBottomColor: "#cac0c0",
        borderTopColor: "#cac0c0",

    },
    Exit: {
        flexDirection: 'row',
        justifyContent: 'space-between',
        marginRight: 0,
        marginLeft: 0,
        marginTop: 20,
        paddingLeft: 10,
        backgroundColor: 'white',
    },
    view: {
        marginTop: 15,
    },
});


